Completed
Pull Request — master (#77)
by
unknown
39s
created

angular.controller(ꞌct_matterꞌ)   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 28
rs 6.7272
1
'use strict';
2
3
angular.module('game').component('matter', {
4
  templateUrl: 'views/matter.html',
5
  controller: 'ct_matter',
6
  controllerAs: 'ct'
7
});
8
9
angular.module('game').controller('ct_matter', ['state', 'visibility', 'data', 'util',
10
function (state, visibility, data, util) {
11
  let ct = this;
12
  ct.state = state;
13
  ct.visibility = visibility;
14
  ct.data = data;
15
16
  function processDecay(player) {
17
    for (let i = 0; i < data.radioisotopes.length; i++) {
18
      let resource = data.radioisotopes[i];
19
      if (player.resources[resource].unlocked) {
20
        let number = player.resources[resource].number;
21
        let halfLife = data.resources[resource].decay.half_life;
22
        let production = util.randomDraw(number, Math.log(2) / halfLife);
23
24
        if (production === 0) {
25
          continue;
26
        }
27
28
        // we decrease the number of radioactive element
29
        player.resources[resource].number -= production;
30
31
        // and decay products
32
        for (let type of data.resources[resource].decay.decay_types) {
33
          for (let product of type.decay_product) {
34
            player.resources[product].number += Math.floor(product * production * type.ratio);
35
            if (!player.resources[product].unlocked) {
36
              player.resources[product].unlocked = true;
37
              visibility.addNew(product);
38
            }
39
          }
40
        }
41
      }
42
    }
43
  }
44
45
  function processGenerators(player) {
46
    // We will simulate the production of isotopes proportional to their ratio
47
    for (let element in player.elements) {
48
      if (player.elements[element].unlocked === false) {
49
        continue;
50
      }
51
      // Prepare an array with the isotopes
52
      let isotopes = Object.keys(data.elements[element].isotopes);
53
      let remaining = ct.elementProduction(element);
54
      // We will create a random draw recalculate the mean and std
55
      for (let i = 0; i < isotopes.length - 1; i++) {
56
        // First we need to adjust the ratio for the remaining isotopes
57
        let remainingRatioSum = 0;
58
        for (let j = i; j < isotopes.length; j++) {
59
          remainingRatioSum += data.resources[isotopes[j]].ratio;
60
        }
61
62
        let p = data.resources[isotopes[i]].ratio / remainingRatioSum;
63
        let production = util.randomDraw(remaining, p);
64
65
        if (production > 0) {
66
          player.resources[isotopes[i]].number += production;
67
          if (!player.resources[isotopes[i]].unlocked) {
68
            player.resources[isotopes[i]].unlocked = true;
69
            visibility.addNew(isotopes[i]);
70
          }
71
        }
72
        remaining -= production;
73
      }
74
      // The last isotope is just the remaining production that hasn't been consumed
75
      if (remaining > 0) {
76
        let last = isotopes[isotopes.length - 1];
77
        player.resources[last].number += remaining;
78
        if (!player.resources[last].unlocked) {
79
          player.resources[last].unlocked = true;
80
          visibility.addNew(last);
81
        }
82
      }
83
    }
84
  }
85
86
  function update(player) {
87
    processDecay(player);
88
    processGenerators(player);
89
  }
90
91
  ct.generatorPrice = function(name, element) {
92
    let level = state.player.elements[element].generators[name];
93
    let price = data.generators[name].price * Math.pow(data.generators[name].priceIncrease, level);
94
    return Math.ceil(price);
95
  };
96
97
  ct.buyGenerators = function(name, element, number) {
98
    let price = this.generatorPrice(name, element);
99
    let i = 0;
100
    // we need a loop since we use the ceil operator
101
    let currency = data.elements[element].main;
102
    while (i < number && state.player.resources[currency].number >= price) {
103
      state.player.resources[currency].number -= price;
104
      state.player.elements[element].generators[name]++;
105
      price = this.generatorPrice(name, element);
106
      i++;
107
    }
108
  };
109
110
  ct.generatorProduction = function(name, element) {
111
    let baseProduction = data.generators[name].power;
112
    return upgradedProduction(baseProduction, name, element);
113
  };
114
115
  ct.tierProduction = function(name, element) {
116
    let baseProduction = data.generators[name].power *
117
      state.player.elements[element].generators[name];
118
    return upgradedProduction(baseProduction, name, element);
119
  };
120
121
  function upgradedProduction(production, name, element) {
122
    for (let up in data.generators[name].upgrades) {
123
      if (state.player.elements[element].upgrades[data.generators[name].upgrades[up]]) {
124
        let power = data.upgrades[data.generators[name].upgrades[up]].power;
125
        production = upgradeApply(production, power);
126
      }
127
    }
128
    let exotic = data.elements[element].exotic;
129
    production += production * state.player.resources[exotic].number * data.constants.EXOTIC_POWER;
130
    return Math.floor(production);
131
  }
132
133
  function upgradeApply(resource, power) {
134
    return resource * power;
135
  }
136
137
  ct.elementProduction = function(element) {
138
    let total = 0;
139
    for (let tier in data.generators) {
140
      total += ct.tierProduction(tier, element);
141
    }
142
    return total;
143
  };
144
145
  state.registerUpdate('matter', update);
146
}]);
147